package com.living.player.db;

import android.text.TextUtils;

import com.md.basedpc.persistence.PHDbUtils;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;

public class RecentlyManger {

    private static RecentlyManger instance = null;

    public static RecentlyManger getInstance() {
        if (instance == null) instance = new RecentlyManger();
        return instance;
    }

    public void saveRecently(Album album, Audio audio) {
        saveAlbum(album);
        saveAudio(album.getId(), audio);
    }

    /**
     * 保存 专辑信息
     *
     * @param album
     * @return
     */
    private static RecentlyAlbumDB saveAlbum(Album album) {
        RecentlyAlbumDB albumDB = new RecentlyAlbumDB();
        albumDB.setId(album.getId());
        albumDB.setAuthor(album.getAuthor());
        albumDB.setName(album.getName());
        albumDB.setCover(album.getCoverUrl());
        albumDB.setDesc(album.getDesc());
        albumDB.setCount(album.getAudios() == null ? 0 : album.getAudios().size());
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumDB.getId()))
            where += " and id=" + albumDB.getId() + "";
        try {
            List<RecentlyAlbumDB> dbs = PHDbUtils.getInstance().query(RecentlyAlbumDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                PHDbUtils.getInstance().insert(albumDB);
            } else {
                return dbs.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存 音频信息
     *
     * @param albumId
     */
    private AudioPlayListDB saveAudio(String albumId, Audio audio1) {
        RecentlyAudioDB audioDB = new RecentlyAudioDB();
        audioDB.setAlbum_id(albumId);
        audioDB.setAlbum_name(audio1.getAlbumName());
        audioDB.setId(audio1.getId());
        audioDB.setName(audio1.getName());
        audioDB.setAuthor(audio1.getAuthor());
        audioDB.setCover(audio1.getCoverUrl());
        audioDB.setDesc(audio1.getDesc());
        audioDB.setUrl(audio1.getAudioUrl());
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioDB.getId()))
            where += " and id=" + audioDB.getId() + "";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id=" + albumId + "";
        try {
            List<AudioPlayListDB> dbs = PHDbUtils.getInstance().query(RecentlyAudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                PHDbUtils.getInstance().insert(audioDB);
            } else {
                return dbs.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 删除 音频信息
     *
     * @param audioId
     * @return
     */
    public boolean deleteAudio(String audioId) {
        boolean isDelSucess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId))
            where += " and id=" + audioId + "";
        try {
            isDelSucess = PHDbUtils.getInstance().delete(RecentlyAudioDB.class, where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    public boolean deleteAlbumById(String albumId) {
        boolean isDelSucess = false;
        try {
            String where = " 1=1 ";
            if (!TextUtils.isEmpty(albumId))
                where += " and album_id=" + albumId + "";
            isDelSucess = PHDbUtils.getInstance().delete(RecentlyAudioDB.class, where);

            String whereAlbum = " 1=1 ";
            if (!TextUtils.isEmpty(albumId))
                whereAlbum += " and id=" + albumId + "";
            isDelSucess = PHDbUtils.getInstance().delete(RecentlyAlbumDB.class, whereAlbum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    /**
     * 读取所有下载音频数据
     *
     * @return
     */
    public List<Audio> getAllRecentAudios() {
        final List<Audio> list = new ArrayList<>();
        String where = " 1=1 ";
        where += " order by id asc ";
        try {
            List<RecentlyAudioDB> dbs = PHDbUtils.getInstance().query(RecentlyAudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                for (RecentlyAudioDB audioDB : dbs) {
                    Audio model = new Audio(audioDB.getId(),
                            audioDB.getAuthor(),
                            audioDB.getCover(),
                            audioDB.getDesc(),
                            true,
                            audioDB.getName(),
                            audioDB.getUrl());
                    model.setAlbumId(audioDB.album_id);
                    model.setAlbumName(audioDB.getAlbum_name());
                    list.add(model);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 读取所有下载专辑数据
     *
     * @return
     */
    public List<Album> getAllRecentAlbums() {
        final List<Album> list = new ArrayList<>();
        String where = " 1=1 ";
        where += " order by id asc ";
        try {
            List<RecentlyAlbumDB> dbs = PHDbUtils.getInstance().query(RecentlyAlbumDB.class, where);
            if (dbs == null || dbs.size() == 0) {
            } else {
                for (RecentlyAlbumDB audioDB : dbs) {
                    Album model = new Album(new ArrayList<Audio>(),
                            audioDB.getAuthor(),
                            audioDB.getCover(),
                            audioDB.getDesc(),
                            audioDB.getId(),
                            audioDB.getCount(),
                            audioDB.getName(),
                            null);
                    list.add(model);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}
